package org.shopen.puremvc.view
{
	import mx.collections.ArrayCollection;
	import mx.events.ItemClickEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.puremvc.ApplicationConstants;
	import org.shopen.puremvc.model.StagesProxy;
	import org.shopen.puremvc.model.domain.theory.StageMeta;
	import org.shopen.puremvc.view.components.ItemSelector;

	public class StageSelectorMediator extends Mediator
	{
		public static const NAME : String = "StagesSelectorMediator";
		
		public function StageSelectorMediator(viewComponent:Object=null)
		{
			super(StageSelectorMediator.NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			//sendNotification(ApplicationConstants.LOAD_STAGES);
		}
		
		override public function getMediatorName():String
		{
			return StageSelectorMediator.NAME;
		}
		
		[Bindable]
		private var _stages : ArrayCollection = new ArrayCollection();
		[Bindable]
		protected function get stages() : ArrayCollection
		{
			return _stages;
		} 
		protected function set stages(value : ArrayCollection) : void
		{
			_stages = value;
		}
		
		protected function get selector() : ItemSelector
		{
			return viewComponent as ItemSelector;
		}
		
		protected function get selectedIndex() : Number
		{
			var result : Number = selector.selectedIndex;
			return result;
		}
		protected function set selectedIndex(value : Number) : void
		{
			if (value >= 0)			
				selector.selectedIndex = value;
			else
				selector.selectedIndex = -1; //deselect
		}
		
		protected function get currentItem() : StageMeta
		{
			var result : StageMeta = null;
			if (selectedIndex != -1)
			{
				result = stages.getItemAt(selectedIndex) as StageMeta;
			}
			return result;
		}
		
		override public function listNotificationInterests():Array
		{
			return [StagesProxy.STAGES_META_INITIALIZED,
					ApplicationConstants.SELECT_TESTING,
					ApplicationConstants.START_SCORING,
					ApplicationConstants.START_TESTING];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case StagesProxy.STAGES_META_INITIALIZED:
					initStages(notification.getBody() as ArrayCollection);
					break;
				case ApplicationConstants.START_TESTING:
				case ApplicationConstants.SELECT_TESTING:
					selectedIndex = -1;
					selector.enabled = false;
					break;				
				case ApplicationConstants.START_SCORING:
					selector.enabled = true;
					break;
			}
		}
		
		private function initStages(stagesArr : ArrayCollection) : void
		{
			stages = stagesArr;
			selector.dataProvider = stages;
			selector.labelField = "title";
			selector.toolTipField = "title";
			selector.addEventListener(ItemClickEvent.ITEM_CLICK, itemClickListener);
		}
		
		private function itemClickListener(event : ItemClickEvent) : void
		{
			if (selectedIndex != -1)
			{
				sendNotification(ApplicationConstants.SELECT_STAGE, currentItem);				
			}
		}
	}
}